package com.gaogzhen.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.gaogzhen.utils.CurrentUser;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * 自动填充
 * @author Administrator
 */
@Component
public class AutoFillHandler implements MetaObjectHandler {
    /**
     * 插入元对象公共字段填充
     * 新增时要填充的字段:
     * 1. 创建人: createBy
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        // 通过安全上下文获取用户Id
        Long userId = CurrentUser.getCurrentUserId();
        this.strictInsertFill(metaObject, "createBy", Long.class, userId);
    }



    /**
     * 修改元对象公共字段填充
     * 修改时要填充的字段:
     * 1. 修改人: modifyBy
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        Long userId = CurrentUser.getCurrentUserId();
        this.strictUpdateFill(metaObject, "modifyBy", Long.class, userId);
    }
}
